<template>
    <div class="login-bg">
        <div class="login-center">
            <!-- <img src="/logo.png" alt="ERP系统" class="login-logo" /> -->
            <div class="login-title">企业资源计划（ERP）系统</div>
            <el-card class="login-card">
                <template #header>
                    <div class="card-header">
                        <span>欢迎登录</span>
                    </div>
                </template>
                <el-form label-width="80px" :model="data">
                    <el-form-item label="用户名">
                        <el-input v-model="data.admin" placeholder="请输入用户名" />
                    </el-form-item>
                    <el-form-item label="密码">
                        <el-input v-model="data.pwd" type="password" placeholder="请输入密码" show-password />
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="login" type="primary" class="login-button">登录</el-button>
                    </el-form-item>
                </el-form>
            </el-card>
        </div>
    </div>
</template>

<script lang="ts" setup>
import axios from 'axios'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'
import { useCounterStore } from '../stores/counter'
import { read } from '../stores/host'

const user = useCounterStore()
const router = useRouter()

const login = () => {
    if (!data.admin) {
        ElMessage.error("用户名不能为空")
        return
    }
    if (!data.pwd) {
        ElMessage.error("密码不能为空")
        return
    }
    read.post("/authorize/api/Management/Login", data).then(res => {
        if (res.data.code == 100) {
            user.user.id = res.data.data.id
            user.user.name = res.data.data.name
            user.user.workid = res.data.data.workId
            user.user.Administrator = res.data.data.administrator
            user.user.role = res.data.data.rids.toString()
            localStorage.setItem("token", res.data.data.token)
            axios.defaults.headers.common["Authorization"] = "Bearer " + res.data.data.token
            ElMessage.success("登录成功")
            router.push("/Main")
        } else {
            ElMessage.error(res.data.message)
        }
    })
}

const data = reactive({
    admin: "",
    pwd: ""
})
</script>

<style scoped>
.login-bg {
    min-height: 100vh;
    width: 100vw;
    background: linear-gradient(135deg, #f0f4f8 0%, #e9ecef 100%);
    display: flex;
    align-items: center;
    justify-content: center;
}

.login-center {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
}

/* .login-logo {
  width: 64px;
  height: 64px;
  margin-bottom: 16px;
} */
.login-title {
    font-size: 2rem;
    font-weight: bold;
    color: #409EFF;
    margin-bottom: 32px;
    letter-spacing: 2px;
    text-shadow: 0 2px 8px rgba(64, 158, 255, 0.08);
}

.login-card {
    width: 400px;
    border-radius: 16px;
    box-shadow: 0 4px 24px rgba(64, 158, 255, 0.10);
    background: #fff;
}

.card-header {
    text-align: center;
    font-size: 22px;
    font-weight: bold;
    color: #409EFF;
    letter-spacing: 1px;
}

.login-button {
    width: 100%;
    font-size: 1.1rem;
    border-radius: 8px;
    height: 40px;
}

.el-form-item {
    margin-bottom: 24px;
}

@media (max-width: 600px) {
    .login-card {
        width: 95vw;
        min-width: 0;
        padding: 8px;
    }

    .login-title {
        font-size: 1.2rem;
        margin-bottom: 16px;
    }

    .login-logo {
        width: 40px;
        height: 40px;
        margin-bottom: 8px;
    }
}
</style>